#include <rdma/rdma_cma.h>
#include <rdma/rdma_verbs.h>

#define MAX_SGE 8
#define MAX_MR MAX_SGE
#define MR_LEN  4096
#define IB_PORT 1

enum ep_mode {
	EP_SERVER,
	EP_CLIENT
};

class RDMAUtil{
private:
  	struct rdma_event_channel *ec;
	struct rdma_cm_id *id;
	struct ibv_mr *mr;
	byte* rdmaBuffer;

	/**
	 * get_context- Open specific IB device to get ibv_context.
	 */
	struct ibv_context* get_context();

	/**
	 * build_ep - Create cq, qp, etc. for the given communication identifier.
	 */
	int build_ep();


	void initClient(int port);

	void initServer(int port);

public:
	RDMAUtil(ep_mode mode, int port);

	~RDMAUtil();

	/**
	 * Send Length of rdma read/write.
	 */
	int send_length(int length);

	/**
	 * Receive Length of rdma read/write.
	 * @length: A reference of number length will be returned.
	 */
	int recv_length();

	/**
	 * Send data.
	 * @buffer: Memory region registered.
	 * @bufLen: length of data to send.
	 */
	int send(byte *buffer, int bufLen);

	/**
	 * Receive data.
	 * @buffer: Memory region registered.
	 * @bufLen: length of data to recv.
	 */
	int recv(byte *buffer, int bufLen);

	/**
	 * one call of send_length and send.
	 * @buffer: Memory region registered.
	 * @bufLen: length of data to send.
	 */
	int send2(byte *buffer, int bufLen);

	/**
	 * One call of recv_length and recv.
	 * @buffer: Memory region registered.
	 */
	int recv2(byte *buffer);
};
